#include <jni.h>
#include <string>
#include "fker_gutil.h"
#include "fker_constants.h"
#include <android/log.h>
#include <sys/system_properties.h>


extern "C"
JNIEXPORT void JNICALL
Java_com_fker_opt_JNIFKerLibs_send(JNIEnv *env, jclass clazz, jobject view_group, jint x, jint y) {

    jclass j_cls_opt_native = env->FindClass("com/fker/opt/OptNativeLibs");
    if (j_cls_opt_native == NULL) {
        return;
    }
    if (view_group == NULL) {
        return;
    }

    jclass j_cls_view = env->FindClass("android/view/View");
    jmethodID jm_view_get_content = env->GetMethodID(j_cls_view, "getContext",
                                                     "()Landroid/content/Context;");
    jobject context = env->CallObjectMethod(view_group, jm_view_get_content);


    jclass js_content_cls = env->FindClass("android/content/Context");

    jmethodID jm_pk_name = env->GetMethodID(js_content_cls, "getPackageName",
                                            "()Ljava/lang/String;");
    jstring js_pk_name = (jstring) env->CallObjectMethod(context, jm_pk_name);

    const char *pkName = env->GetStringUTFChars(js_pk_name, 0);

    const char *filter_pk_name = FILTER_PACKAGE_NAME

    LOGD("executeName: %s", pkName);

    if (strcmp(pkName, filter_pk_name) == 0) {
        LOGI("---包名检测成功");
        jmethodID sendMId = env->GetStaticMethodID(j_cls_opt_native, "sCallBack",
                                                   "(Landroid/view/ViewGroup;II)V");
        //execute 模拟调用
        env->CallStaticVoidMethod(j_cls_opt_native, sendMId, view_group, x, y);
    } else {
        LOGI("---error：包名验证失败 ----");
    }


    env->DeleteLocalRef(j_cls_opt_native);
    env->DeleteLocalRef(j_cls_view);
    env->DeleteLocalRef(context);
    env->DeleteLocalRef(js_content_cls);
    env->DeleteLocalRef(js_pk_name);

}